<template>
    <el-container class="Home-container">
       <!-- 头部区域 -->
      <el-header>
        <div>
          <img src="../assets/logoko.png" alt="">
          <span>个人博客后台管理系统</span>
        </div>
        <el-button type="primary" @click="Exit" class="iconfont icon-sign-out2">退出</el-button>
      </el-header>
      <!-- 主体部分 -->
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '60px' : '200px' ">
          <div class="toggle-button" @click="toggleCollapse">|||</div>
          <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-openeds="openeds"
          :default-active="activeUrl">
          <!-- 一级菜单 -->
          <el-submenu index="1">
            <template slot="title">
              <i class="iconfont icon-caidan"></i>
              <span class="MenuBar">菜单栏</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item @click="saveNavState('/' + item.side_url)" v-for="item in menulist" :key="item.side_id" :index="'/' + item.side_url">
              <template slot="title">
                <!-- 图标 -->
                <i :class="item.side_Icon"></i>
                <!-- 文本 -->
                <span class="home_name">
                  {{item.side_name}}
                </span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
        </el-aside>
        <!-- 右侧内容主体 -->
        <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单数据
      menulist: [],
      // 是否折叠
      isCollapse: false,
      openeds: ['1'],
      // 被激活的链接地址
      activeUrl: ''
    }
  },
  created () {
    this.getMenuList()
    this.activeUrl = window.sessionStorage.getItem('activeUrl')
  },
  methods: {
    // 实现退出按钮
    Exit () {
      // 清除token
      window.sessionStorage.clear()
      // 赋值链接地址
      this.$router.push('/login')
    },
    // 获取所有的菜单栏
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.status !== 200) return this.$message.error('获取数据失败！')
      this.menulist = res.result
    },
    // 控制菜单的折叠和展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState (activeUrl) {
      window.sessionStorage.setItem('activeUrl', activeUrl)
      // 点击不同的链接赋值不同的激活链接地址
      this.activeUrl = activeUrl
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
  font-weight: bold;
  font-family: 楷体;
  letter-spacing: 1px;
  padding-left: 5px;
  > div {
    display: flex;
    align-items: center;
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #EAEDF1;
}

.Home-container {
  height: 100%;
}

.home_name {
  letter-spacing: 3px;
  font-size: 16px;
  font-family: 楷体;
}

.toggle-button {
  background-color: #4A5064;
  color: #fff;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 2px;
  cursor: pointer;
}

.MenuBar {
  letter-spacing: 3px;
  font-size: 16px;
  font-family: 楷体;
  font-weight: bold;
  margin-left: 10px;
}

.el-main {
  padding: 15px;
}
</style>
